package main

import (
	"fmt"
)

// Sayer 接口就是一个需要实现的方法列表
type Sayer interface {
	say()
}
type dog struct {}
type cat struct {}

func (d dog) say()  {
	fmt.Println("汪汪汪")
}
func (c cat) say() {
	fmt.Println("喵喵喵")
}

type Mover interface {
	move()
}

// Animal 接口嵌套
type Animal interface {
	Sayer
	Mover
}

//指针接收者实现
func (d *dog) move()  {
	fmt.Println("狗会动")
}

func main() {
	//dog cat实现sayer接口
	//var s Sayer
	//var dog = dog{}
	//s = dog
	//s.say()
	//var cat = cat{}
	//s = cat
	//s.say()

	var dog = dog{}
	dog.say()

	var m Mover
	m = &dog	//指针接收者如要传入指针才可以
	m.move()
	var a Animal
	a = &dog
	a.say()
	a.move()

	//空接口
	var k interface{}
	aa := "aaa"
	k = aa
	fmt.Printf("type:%T value:%v\n", k, k)
	mapInterface()

	//断言
	var duanyan interface{}
	duanyan = "aaa"
	if v, ok := duanyan.(string); ok{
		fmt.Println(v)

	}
}

func duanyanfunc(x interface{}) {
	switch v := x.(type) {
	case string:
		fmt.Printf("x is a string，value is %v\n", v)
	case int:
		fmt.Printf("x is a int is %v\n", v)
	case bool:
		fmt.Printf("x is a bool is %v\n", v)
	default:
		fmt.Println("unsupport type！")
	}
}

//空接口作为函数的参数
func show(a interface{})  {
	fmt.Printf("type:%T value:%v\n", a, a)
}
//空接口作为map的值
func mapInterface() {
	var studentInfo = make(map[string]interface{})
	studentInfo["name"] = "古力娜扎"
	fmt.Println(studentInfo)
}
